<?php

namespace App\Http\Resources\Stats\User\Player;

use App\Http\Resources\Resource;
use Helper\Timed;

class DayResource extends Resource
{

	/**
	 * Transform the resource into an array.
	 *
	 * @param \Illuminate\Http\Request $request
	 * @return array
	 */
	public function toArray($request)
	{
		$charts = [
			'timed' => [],
			'data' => [
				'u_game' => [],
				'u_sign' => [],
				'nu_game' => [],
				'nu_sign' => [],
			],
		];

		$datetime = $request->input('datetime');

		list($timed, $timedEnd) = Timed::datetimeFormat1($request,'datetime', $datetime[0], $datetime[1], 'day');

		$stats = $this->resource['stats'];

		date_default_timezone_set(Timed::getTimezoneName());
		foreach ($stats['u_game'] as $k => $v) {
			$stats['u_game'][date('Y.m.d', $k)] = $v;
		}
		foreach ($stats['u_sign'] as $k => $v) {
			$stats['u_sign'][date('Y.m.d', $k)] = $v;
		}
		foreach ($stats['nu_game'] as $k => $v) {
			$stats['nu_game'][date('Y.m.d', $k)] = $v;
		}
		foreach ($stats['nu_sign'] as $k => $v) {
			$stats['nu_sign'][date('Y.m.d', $k)] = $v;
		}
		for (; $timed <= $timedEnd; $timed += 86400) {
			$day = date('Y.m.d', $timed);
			// if ($day > date('Y.m.d')) {
			// 	continue;
			// }
			$charts['timed'][] = date('Y.m.d', $timed);
			$charts['data']['u_game'][] = $stats['u_game'][$day] ?? null;
			$charts['data']['u_sign'][] = $stats['u_sign'][$day] ?? null;
			$charts['data']['nu_game'][] = $stats['nu_game'][$day] ?? null;
			$charts['data']['nu_sign'][] = $stats['nu_sign'][$day] ?? null;
		}

		return $this->getResponse()->setResponseData($charts);

	}
}
